package top.swimmer.tools.data;

import java.util.ArrayList;
import java.util.List;

public class PhoneNumber {
    private static String[] arr = new String[]{
      "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
    };

    public static List<StringBuilder> join(String str) {
        List<StringBuilder> result = new ArrayList<>();
        int length = str.length();
        for (int i = 0; i < length; i++) {
            int index = str.charAt(i) - '0';
            String numStr = arr[index];
            int resultLength = 1;
            for (int j = 0; j < numStr.length(); j++) {
                for (StringBuilder s : result) {
                    if (s == null) {
                        s = new StringBuilder();
                        result.add(s);
                    }
                    s.append(numStr.charAt(j));
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
        letterCombinations("");
        System.out.println(allResult);
    }

    static List<String> allResult = new ArrayList<>();

    public static List<String> letterCombinations(String digits) {
        if (digits != null && !"".equals(digits)) {
            join1("", digits);
        }
        return allResult;
    }

    private static void join1(String result, String str) {
        if (str.length() == 0) {
            allResult.add(result);
        } else {
            int index = str.charAt(0) - '0';
            String numStr = arr[index];
            String lastStr = str.substring(1);
            for (int i = 0; i < numStr.length(); i++) {
                join1(result + numStr.charAt(i), lastStr);
            }
        }
    }
}
